# coding=utf-8
import base64, hmac
import hashlib


class S3SignatureService(object):
    """
    创建客户端上传文件使用的签名
    """
    def __init__(self):
        """

        """

    def dev_profile_signature(self):
        """
        dev 个人资料相关

        :return:
            base64_policy: 权限(base64)
            signature: 签名
        """
        secret_access_key_id = 'AKIAICK5LDEK6CPDVFQQ'
        secret_access_key = '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'

        policy_str = '''{"expiration": "2029-01-01T00:00:00Z",
  "conditions": [ 
    {"bucket": "oregon-service"}, 
    ["starts-with", "$key", "test/profile"],
    ["starts-with", "$Content-Type", ""],
    ["content-length-range", 0, 31457280]
  ]
}'''

        utf8_policy = policy_str.encode('utf-8')
        base64_policy = base64.b64encode(utf8_policy)

        print('base64_policy: ')
        print(base64_policy)

        signature = hmac.new(secret_access_key.encode('utf-8'), base64_policy, hashlib.sha1).digest()
        signature = base64.b64encode(signature)

        print('signature: ')
        print(signature)

        return base64_policy, signature


    def dev_article_signature(self):
        """
        dev 帖子相关

        :return:
            base64_policy: 权限(base64)
            signature: 签名
        """
        secret_access_key_id = 'AKIAJUJLB6B5OKFPFYAQ'
        secret_access_key = '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'

        policy_str = '''{"expiration": "2029-01-01T00:00:00Z",
  "conditions": [ 
    {"bucket": "oregon-service"}, 
    ["starts-with", "$key", "test/article/upload"],
    ["starts-with", "$Content-Type", ""],
    ["content-length-range", 0, 31457280]
  ]
}'''

        utf8_policy = policy_str.encode('utf-8')
        base64_policy = base64.b64encode(utf8_policy)

        print('base64_policy: ')
        print(base64_policy)

        signature = hmac.new(secret_access_key.encode('utf-8'), base64_policy, hashlib.sha1).digest()
        signature = base64.b64encode(signature)

        print('signature: ')
        print(signature)

        return base64_policy, signature


    def dev_feedback_signature(self):
        """
        dev 用户反馈信息相关

        :return:
            base64_policy: 权限(base64)
            signature: 签名
        """
        secret_access_key_id = 'AKIAIN7DDLUM65KT2YQA'
        secret_access_key = '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'

        policy_str = '''{"expiration": "2029-01-01T00:00:00Z",
  "conditions": [ 
    {"bucket": "oregon-service"}, 
    ["starts-with", "$key", "test/feedback"],
    ["starts-with", "$Content-Type", ""],
    ["content-length-range", 0, 31457280]
  ]
}'''

        utf8_policy = policy_str.encode('utf-8')
        base64_policy = base64.b64encode(utf8_policy)

        print('base64_policy: ')
        print(base64_policy)

        signature = hmac.new(secret_access_key.encode('utf-8'), base64_policy, hashlib.sha1).digest()
        signature = base64.b64encode(signature)

        print('signature: ')
        print(signature)

        return base64_policy, signature


if __name__ == '__main__':
    pass
    # r = S3SignatureService().dev_profile_signature()
    # r = S3SignatureService().dev_article_signature()
    r = S3SignatureService().dev_feedback_signature()